<?php
require_once 'classes/photos.php';
require_once 'classes/database.php';

class Album{
	protected $id, $owner, $label, $cover;

	static $dbTableName = "pg_album";
	static $dbTableNameGeo = "pg_geoalbum";
	static $currentAlbumForms = "currentAlbumId";
	static $currentAlbumLabelForms = "currentAlbumLabel";

	function __construct($idN,$ownerN, $labelN){
		$this->id = $idN;
		$this->owner = $ownerN;
		$this->label = $labelN;
	}

	function setLabel($new){
		$this->label = $new;
	}

	function setOwner($new){
		$this->owner = $new;
	}

	function setCover($new){
		$this->cover = $new;
	}

	function getId(){
		return($this->id);
	}

	function getOwner(){
		return($this->owner);
	}

	function getLabel(){
		return($this->label);
	}

	function getCover(){
		if(!is_null($this->cover))
			return("./photos/".$this->getOwner()."/thumbs/".$this->cover);
		else
			return('./images/sem-imagem.jpg');
	}

}

class AlbumDBUtils{
	private $dbUtils;

	function __construct(){
		$this->dbUtils = new DatabaseUtils();
	}

	function __destruct(){
		unset($database);
	}

	function albumFromRow($row){
		if(
				isset($row["owner"]) && isset($row["id"]) && isset($row["label"])
				&& !(trim($row["label"])==='') && !(trim($row["id"])==='') && !(trim($row["owner"])==='')
		){
			$a = new Album($row["id"],$row["owner"],$row["label"]);
			$a->setCover($row["cover"]);
			return($a);
		}else{
			return(NULL);
		}
	}

	function findUserAlbums($uid,$limit){
		$q = "SELECT * FROM ".Album::$dbTableName." WHERE owner = :owner";
		$params =  array(":owner"=>$uid);
		if($limit>0){
			$q = $q." LIMIT 0,:limit";
			$params[":limit"]=$limit;
		}

		$result = $this->dbUtils->runSelect($q, $params);
		if(count($result)<1){
			return(array("status"=>FALSE,"message"=>"No album found."));
		}else{
			return(array("status"=>TRUE,"message"=>"Albums found.", "albums"=>$result));
		}
	}

	function setCoverPhoto($pid,$albumid){
		$q = "UPDATE ".Album::$dbTableName." a SET cover=(SELECT p.file_name FROM ".Photo::$dbTableName." p WHERE p.id=:pid) WHERE a.id=:albumid";
		$params = array(":pid" => $pid, ":albumid"=>$albumid);
		$resultSave = $this->dbUtils->runUpdate($q, $params);
		if($resultSave>0){
			return(array("status"=>TRUE, "message"=>"Saved."));
		}else{
			return(array("status"=>FALSE, "message"=>("Couldn't save album cover.")));
		}
	}

	function saveAlbum($a){
		$q = "SELECT * FROM ".Album::$dbTableName." WHERE label = :label AND owner = :owner";
		$params = array(":label"=>$a->getLabel(), ":owner"=>$a->getOwner());
		$result = $this->dbUtils->runSelect($q, $params);
		if(count($result)<1){
			$q = "INSERT INTO ".Album::$dbTableName."(owner,label) VALUES(:owner,:label)";
			$params = array(":label"=>$a->getLabel(), ":owner"=>$a->getOwner());
			$resultSave = $this->dbUtils->runInsert($q, $params);

			$q = "SELECT * FROM ".Album::$dbTableName." WHERE label = :label AND owner = :owner";
			$params = array(":label"=>$a->getLabel(), ":owner"=>$a->getOwner());
			$result = $this->dbUtils->runSelect($q, $params);
			foreach ($result as $album){
				$a = $this->albumFromRow($album);
			}
		}else{
			foreach ($result as $album){
				$q = "UPDATE ".Album::$dbTableName." SET label=:label, cover=:cover WHERE id=:id";
				$params = array(":label"=>$album->getLabel(),":cover"=>$album->getCover(), ":id"=>$album->getId());
				$resultSave = $this->dbUtils->runUpdate($q, $params);
			}
		}
		if($resultSave>0){
			return(array("status"=>TRUE, "message"=>"Saved.", "album"=>$a));
		}else{
			return(array("status"=>FALSE, "album"=>$a, "message"=>("Couldn't save album with label: ".$a->getLabel())));
		}
	}

	static function cmp_geoAlbuns($a,$b){
		if ($a["level"] == $b["level"]) {
			return 0;
		}
		return ($a["level"] < $b["level"]) ? -1 : 1;
	}

	function saveGeoAlbuns($geoAlbuns){
		$result = array("status"=>false);
		if(!is_null($geoAlbuns) && !empty($geoAlbuns)){
			usort($geoAlbuns, array ("AlbumDBUtils", "cmp_geoAlbuns"));

			$result["status"] = true;

			$parent = null;
			foreach($geoAlbuns as $a){
				$a["parent"] = $parent;
				$result["status"] = $this->saveGeoAlbum($a)["status"] && $result["status"];
				$parent = $a["id"];
			}
		}
		return $result;
	}

	function saveGeoAlbum($a){
		$q = "SELECT * FROM ".Album::$dbTableNameGeo." WHERE id = :id";
		$params = array(":id"=>$a["id"]);
		$result = $this->dbUtils->runSelect($q, $params);
		if(count($result)<1){
			$q = "INSERT INTO ".Album::$dbTableNameGeo."(id,name,level,translation,parent) VALUES(:id,:name,:level,:translation,:parent)";
			$params = array(":id"=>$a["id"], ":name"=>$a["name"], ":level"=>$a["level"], ":translation"=>$a["translation"], ":parent"=>$a["parent"]);
			$resultSave = $this->dbUtils->runInsert($q, $params);

		}else{
			foreach ($result as $album){
				$q = "UPDATE ".Album::$dbTableNameGeo." SET name=:name,level=:level,translation=:translation,parent=:parent WHERE id=:id";
				$params = array(":id"=>$a["id"], ":name"=>$a["name"], ":level"=>$a["level"], ":translation"=>$a["translation"], ":parent"=>$a["parent"]);
				$resultSave = $this->dbUtils->runUpdate($q, $params);
			}
		}
		if($resultSave>0){
			return(array("status"=>TRUE, "message"=>"Saved."));
		}else if($resultSave==0){
			return(array("status"=>TRUE, "message"=>("No changes made.")));
		}else{
			return(array("status"=>FALSE, "message"=>("System error, try again later.")));
		}
	}

	function getUserGeoAlbums($owner){
		$q = "SELECT * FROM ".Album::$dbTableNameGeo." WHERE id in (SELECT distinct(ga_p.geoalbumid) as gaid FROM ".Photo::$dbTableName." p, ".Photo::$dbTableNameGeoAlbuns." ga_p WHERE ga_p.photoid = p.id AND p.owner = :owner )";
		$params = array(":owner"=>$owner);
		$result = $this->dbUtils->runSelect($q, $params);
		if(count($result)<1){
			return(array("status"=>FALSE,"message"=>"No album found."));
		}else{
			return(array("status"=>TRUE,"message"=>"Photos found.", "geoalbums"=>$result));
		}
	}
}

$albumUtils = new AlbumDBUtils();
